package edu.sctu.zhoumengxue.day201506.day20150628.test;

import java.io.*;

/**将数据写入文件中和读取出来
 * 使用序列化和反序列化
 * Created by user12 on 2015/6/28.
 */
public class FileURL {

    public void setDocuments(String path) throws IOException{

        File file = new File(path);
        if (!file.exists()){
            file.mkdir();
        }
    }
//将解析到的数据放到Json.txt中
    public void setJsonFile(JsonAppbean appbean,String param) throws IOException{

        File file = new File(param);
        if (!file.exists()){
            file.createNewFile();
        }
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
        out.writeObject(appbean);
        out.close();

    }

    //序列化到Appbean.txt中
    public void setFile(JsonAppbean appbean,String param) throws IOException{

        File file=new File(param) ;
        if (!file.exists()){
            file.createNewFile();
        }
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
        outputStream.writeObject(appbean);
        outputStream.close();

    }

    //反序列化
    public JsonAppbean getFile(String param) throws IOException{

        JsonAppbean appbean = new JsonAppbean();

        File file = new File(param);
        if (!file.exists()){
            file.createNewFile();
        }
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
        try {
            appbean = (JsonAppbean) inputStream.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return appbean;
    }

}
